/*
 * TransiCast City Visions
 * (c) 2014 Joachim Pfeiffer
 * www.transicast.com
 */
package com.transicast.cityvisions;

import java.util.Random;

public class Utilities {

	protected final static String						_TAG = "TransiCastCityVisions";

	public static double distance(double lat1, double lon1, double lat2, double lon2) {
        double theta = lon1 - lon2;
        double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
        dist = Math.acos(dist);
        dist = rad2deg(dist);
        dist *= 111.18957696d;
        return (dist);
    }
    public static double deg2rad(double deg) {
    	return (deg * Math.PI / 180.0d);
    }
    public static double rad2deg(double rad) {
       return (rad * 180.0d / Math.PI);
    }

    public static int random(int min, int max) {
    	Random rand  = new Random();
    	return rand.nextInt((max - min) + 1) + min;
    }

    public static String[] collapseRouteAndDesc(String route, String desc) {
    	if (route == null)
    		route = "";
    	if (desc == null)
    		desc = "";
    	route = route.trim();
    	try {
    		int routeInt = Integer.parseInt(route);
    		route = "" + routeInt;
    	} catch (Exception e) { }
    	desc = desc.trim();
    	if (route.toLowerCase().equals(desc.toLowerCase()))
    		route = "";
    	else
    		if (route.length() > 0 && desc.length() > 0) {
    			while (desc.toLowerCase().substring(0, Math.min(desc.length(), route.length())).equals(route.toLowerCase()) 
    				|| ("0" + desc.toLowerCase().substring(0, Math.min(desc.length(), route.length()))).equals(route.toLowerCase()))
    				desc = desc.substring(route.length(), desc.length()).trim();
    			if (desc.length() > route.length() && desc.indexOf(' ') > 0
    				&& route.length() >= desc.indexOf(' ')
    				&& desc.substring(0, desc.indexOf(' ')).toLowerCase().equals(route.substring(0, desc.indexOf(' ')).toLowerCase()))
    				desc = desc.substring(desc.indexOf(' '), desc.length()).trim();
    			if (desc.length() > route.length() && desc.indexOf(' ') > 0
        			&& route.length() >= desc.length() - (desc.lastIndexOf(' ') + 1)
        			&& desc.substring(desc.lastIndexOf(' ') + 1, desc.length()).toLowerCase().
        				equals(route.substring(0, desc.length() - (desc.lastIndexOf(' ') + 1)).toLowerCase()))
        			desc = desc.substring(0, desc.lastIndexOf(' ')).trim();
    		}
    	String[] result = {route, desc};
    	return result;
    }
}
